import store from '@/store/index.js';
import md5 from '../md5.js';
import { getFileNameUUID } from '../common.js';
import { bigMinus } from '../digital-computation.js';
import axios from "axios";

/**
 * 淘宝埋点
 * 文档：https://taoworld.yuque.com/org-wiki-taoworld-chtafe/lbfw6g/dguu45hgmiczo6mf?singleDoc#Raouo
 * 例子：https://distributor.taobao.global/usertrack?app=123&user=456&page=item_detail&type=PAGE&event=ENTER&args=from%3Dsearch%26id%3D6000123%26duration%3D1300
 * @param url https://distributor.taobao.global/usertrack 请求地址
 * @param app 开放平台appkey
 * @param leadsId 分销商标识
 * @param user 用户唯一id，未登录也要保证唯一
 * @param platform 访问平台 h5
 * @param time 埋点当前时间
 * @param page 埋点所在页面 home_page：首页 search_result：搜索结果页 item_detail：商品详情页 pic_search_result：图搜结果页 OTHER：其他
 * @param type 埋点类型 PAGE：页面浏览 CLK：控件点击 EXP：元素曝光 OTHER：其他
 * @param event 埋点事件 ENTER：进入页面 LEAVE：离开页面 search：搜索 pic_search：图搜 item_view_detail：查看详情 item：商品卡片 block：页面组件
 * @param args 埋点参数，需要编码encodeURIComponent()
 */
const appkey = '501554';
const leadsId = '2100008079133';
const platform = 'PC';
const token = localStorage.getItem('USER_TOKEN');
var times = 0;//记录停留时间ms

const tbRequest = (data) => {
	let url = window.location.href;
	//本地不进行埋点，因为会出现跨域问题
	if (url.includes('localhost')) {
		return;
	}
	axios.get('https://distributor.taobao.global/usertrack', {
		params: data
	})
}

// 公共参数
const commonPage = (page, type, event, time, args) => {
	let user = token ? store._modules.root.state.user.userInfo.id : getFileNameUUID();
	return {
		app: appkey,
		leadsId,
		user: md5.hex_md5(user),
		platform,
		page,
		type,
		event,
		time,
		args
	}
}

/**
 * 首页埋点
 */
// 进入首页
export function toHomePage() {
	let time = new Date().getTime();
	times = time;
	let args = 'from=home_page';
	tbRequest(commonPage('home_page', 'PAGE', 'ENTER', time, encodeURIComponent(args)));
}

// 离开首页
export function leaveHomePage() {
	let time = new Date().getTime();
	let duration = bigMinus(time, times);//停留时长
	let args = `duration=${duration}`;
	tbRequest(commonPage('home_page', 'PAGE', 'LEAVE', time, encodeURIComponent(args)));
}

// 搜夜搜索点击 - 文字搜索 keyword关键词
export function homeSeatchText(keyword) {
	let time = new Date().getTime();
	let args = `keyword=${keyword}`;
	tbRequest(commonPage('home_page', 'CLK', 'search', time, encodeURIComponent(args)));
}

// 搜夜搜索点击 - 图片搜索
export function homeSeatchImg(pic) {
	let time = new Date().getTime();
	let args = `pic=${pic}`;
	tbRequest(commonPage('home_page', 'CLK', 'pic_search', time, encodeURIComponent(args)));
}


/**
 * 搜索页埋点
 */
// 进入搜索页 - 文字
export function toSearchPage(keyword, from) {
	let time = new Date().getTime();
	times = time;
	let args = `keyword=${keyword}&from=${from}`;
	tbRequest(commonPage('search_result', 'PAGE', 'ENTER', time, encodeURIComponent(args)));
}

// 离开搜索页 - 文字
export function leaveSearchPage(keyword, from) {
	let time = new Date().getTime();
	let duration = bigMinus(time, times);//停留时长
	let args = `keyword=${keyword}&from=${from}&duration=${duration}`;
	tbRequest(commonPage('search_result', 'PAGE', 'LEAVE', time, encodeURIComponent(args)));
}

// 搜索按钮点击 - 文字to文字 no页码
export function searchPageText(keyword, keyword_old, no) {
	let time = new Date().getTime();
	let args = `keyword=${keyword}&no=${no}&keyword_old=${keyword_old}`;
	tbRequest(commonPage('search_result', 'CLK', 'search', time, encodeURIComponent(args)));
}

// 搜索按钮点击 - 文字to图片 no页码
export function searchPageImg(pic, keyword_old, no) {
	let time = new Date().getTime();
	let args = `pic=${pic}&no=${no}&keyword_old=${keyword_old}`;
	tbRequest(commonPage('search_result', 'CLK', 'pic_search', time, encodeURIComponent(args)));
}

// 进入搜索页 - 图片
export function toSearchPageImg(pic, from) {
	let time = new Date().getTime();
	times = time;
	let args = `pic=${pic}&from=${from}`;
	tbRequest(commonPage('pic_search_result', 'PAGE', 'ENTER', time, encodeURIComponent(args)));
}

// 离开搜索页 - 图片
export function leaveSearchPageImg(pic, from) {
	let time = new Date().getTime();
	let duration = bigMinus(time, times);//停留时长
	let args = `pic=${pic}&from=${from}&duration=${duration}`;
	tbRequest(commonPage('pic_search_result', 'PAGE', 'LEAVE', time, encodeURIComponent(args)));
}

// 搜索按钮点击 - 图片to文字 no页码
export function searchPageImgText(keyword, pic_old, no) {
	let time = new Date().getTime();
	let args = `keyword=${keyword}&no=${no}&pic_old=${pic_old}`;
	tbRequest(commonPage('pic_search_result', 'CLK', 'search', time, encodeURIComponent(args)));
}

// 搜索按钮点击 - 图片to图片 no页码
export function searchPageImgImg(pic, pic_old, no) {
	let time = new Date().getTime();
	let args = `pic=${pic}&no=${no}&pic_old=${pic_old}`;
	tbRequest(commonPage('pic_search_result', 'CLK', 'pic_search', time, encodeURIComponent(args)));
}

// 搜索结果商品曝光
export function searchListShow(keyword, items) {
	let time = new Date().getTime();
	let args = `keyword=${keyword}&items=${items}`;
	tbRequest(commonPage('search_result', 'EXP', 'item', time, encodeURIComponent(args)));
}

// 搜索结果商品点击
export function searchListClick(keyword, no, pos, item_id) {
	let time = new Date().getTime();
	let args = `keyword=${keyword}&no=${no}&pos=${pos}&item_id=${item_id}`;
	tbRequest(commonPage('search_result', 'CLK', 'item', time, encodeURIComponent(args)));
}



/**
 * 详情埋点
 */
// 进入详情页
export function toDetailPage(item_id, from) {
	let time = new Date().getTime();
	times = time;
	let args = `item_id=${item_id}&from=${from}`;
	tbRequest(commonPage('item_detail', 'PAGE', 'ENTER', time, encodeURIComponent(args)));
}

// 离开详情页
export function leaveDetailPage(item_id, from) {
	let time = new Date().getTime();
	let duration = bigMinus(time, times);//停留时长
	let args = `item_id=${item_id}&from=${from}&duration=${duration}`;
	tbRequest(commonPage('item_detail', 'PAGE', 'LEAVE', time, encodeURIComponent(args)));
}

/**
 * 详情按钮埋点
 * @param item_id 商品id
 * @param type EXP曝光 CLK点击
 * @param block buy_icon购买按钮 addcart_icon加入购物车按钮
 */
export function detailPointBtn(item_id, type, block) {
	let time = new Date().getTime();
	let args = `item_id=${item_id}`;
	let data = {
		...commonPage('item_detail', type, 'block', time, encodeURIComponent(args)),
		block
	}
	tbRequest(data);
}




// 获取埋点上一页面的名称
export function getPrvPage() {
	let pages = getCurrentPages();
	let pagesKey = {
		'pages/home/index': 'home_page',
		'pages/goods/index': 'search_result',
		'pages/goods/deails': 'item_detail'
	}
	if (pages.length <= 1) {
		return 'OTHER'
	} else {
		let prvPage = pages[pages.length - 2];
		if (pagesKey[prvPage.route]) {
			return pagesKey[prvPage.route]
		} else {
			return 'OTHER'
		}
	}
}